package com.imcoke.abchat.common;
import com.imcoke.abchat.domain.User;
import com.imcoke.abchat.web.service.UserDetailsImpl;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ccoke
 * @Description 用于将User转换成UserDetailsImpl
 * @Date: 8:56 2017-10-30
 */
@Data
@NoArgsConstructor
public class UserDetailsFactory {

    public static UserDetailsImpl create(User user){
        return new UserDetailsImpl(user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getModifytime(),
                map2GrantedAuthority(user.getRoles())
        );
    }

    public static List<GrantedAuthority> map2GrantedAuthority(List<String> roles){
        return roles.stream().map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}
